package com.ssm.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.ssm.controller.BaseController;
import com.ssm.entity.Departmentname;
import com.ssm.entity.RecruitInfo;
import com.ssm.service.DepartmentService;
import com.ssm.service.NoticeListService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
public class AdminNoticeController extends BaseController {
    @Resource(name = "noticeList")
    private NoticeListService noticeListService;
    @Resource(name="Departmentname")
    private DepartmentService departmentService;
    // 查询
    @RequestMapping(value ="notice/manage",method = RequestMethod.GET)
    public String NoticeMageageQuery(HttpSession session, Map<String,Object> map){
        List<RecruitInfo> recruitInfos=new ArrayList<>();
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("adminId")){
            return "redirect:/";
        }
        recruitInfos=noticeListService.listAllNoticeList();
        map.put("recruitInfos",recruitInfos);
        List<Departmentname> departmentname= departmentService.listAllDepartment();
        map.put("department",departmentname);
        for (int i = 0; i < recruitInfos.size(); i++) {
            logger.info(recruitInfos.get(i));
        }
        return "admin/notice";
    }
    //添加
    @ResponseBody
    @RequestMapping(value = "notice/add",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String add(HttpSession session, @RequestParam Integer Infoid, @RequestParam String Deparmentid, @RequestParam String name,  @RequestParam Integer number, @RequestParam String place){
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("adminId")){
            return "redirect:/";
        }
        RecruitInfo recruitInfo=new RecruitInfo();
        recruitInfo.setInfoid(Infoid);
        recruitInfo.setDeparmentid(Deparmentid);
        recruitInfo.setName(name);
        recruitInfo.setNumber(number);
        recruitInfo.setPlace(place);
        Integer IsSuccuess=noticeListService.createNoticeList(recruitInfo);
        JSONObject object=new JSONObject();
        if (IsSuccuess==1){
            logger.info("添加成功");

            object.put("status","success");
        }else{
            logger.info("添加错误");
            object.put("status","false");
        }
        return object.toJSONString();
    }
    //删除
    @ResponseBody
    @RequestMapping(value = "notice/delete",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String delete(HttpSession session,  @RequestParam Integer id){
        logger.info("检查管理员权限");
        String adminId=checkAdmin(session);
        if (!adminId.equals("adminId")){
            return "redirect:/";
        }
        Integer IsSuccess=noticeListService.deleteNoticeList(id);
        JSONObject object=new JSONObject();
        if (IsSuccess==1){
            logger.info("删除成功");

            object.put("status","success");
        }else{
            logger.info("删除错误");
            object.put("status","false");
        }
        return object.toJSONString();
    }
}
